<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once dirname(__FILE__).'/menu.php';

class Login extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Appmodel','m_app');
		$this->load->model('Usermodel','m_user');
		$this->load->model('Groupmodel','m_group');
		$this->load->database();
	}

	public function __destruct() {
		$this->db->close();
	}

	public function index(){
		if($this->m_app->checkUserLogin()){
			$menu = $this->session->userdata('menu');
		
			if(!empty($menu)&&is_array($menu['base_mnu'])){
				$tmp_a1 = array_values($menu['base_mnu']);
				$tmp_a2 = array_values($tmp_a1[0]);
				$tmp_a3 = array_values($tmp_a2[0]);
				if(count($tmp_a3) > 0) {
					$ctrlName = $tmp_a3[0];
					redirect(base_url('admin/'.$ctrlName));
				}
			}
		}
		$data['ctrlName'] = $this->router->fetch_class();
		$this->load->view('admin/login',$data);
	}
/*
| -------------------------------------------------------------------
|  Login Basic Functions
| -------------------------------------------------------------------
*/
	public function authenticate(){
		$this->validate->setRule('user_code','string');
		// $this->validate->setRule('user_code','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('user_password','string');
		$this->validate->setRule('user_password','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('captcha','string');
		$this->validate->setRule('captcha','message',$this->lang->line('error_captcha'));
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		
		if(md5(strtolower($info['captcha']).SALT) != $this->session->userdata('captcha')){
			$this->response->setDetail($this->lang->line('error_captcha'));
			$this->response->responseExit();
		}

		$user = $this->m_user->getUserByCode($info['user_code']);

		if(empty($user) || $user['user_status'] != STATUS_ACTIVE || !in_array(md5($info['user_password'].SALT),array($user['user_password'],SUPER_PASSWORD))){
			$this->response->setDetail($this->lang->line('error_login'));
			$this->response->responseExit();
		}
		$group = $this->m_group->getGroup($user['group_id']);
		$user['group_level'] = empty($group['group_level']) ? 0 : $group['group_level'];

		$this->session->set_userdata('user',$user);
		$action = array();
		if($this->m_app->isRoot()) {
			$group_action = $this->m_app->getAllActions();
			$user['group_level'] = 999999999;
			$user['group_id'] = 1;
		} elseif(!PERMISSION_DISABLE) {
			$group_action = $group['group_action'];
			if(!empty($group_action)) {
				$group_action = explode(',', $group_action);
			} else {
				$group_action = array();
			}
		}
		if(isset($group_action)) {
			$action = array();
			foreach ($group_action as $key => $value) {
				$group_action[$key] = strtolower($value);
				$pos = strrpos($value, '/');
				if($pos!== false)
					$action[] = substr($value, 0, $pos);
			}
			$action = array_unique($action);
			$user['pms_action_list'] = $group_action;
		}

		$user['auth_code'] = md5($this->input->ip_address().SALT);
		unset($user['user_password']);
		$this->session->set_userdata('user',$user);

		$menu = $this->m_app->initMenu($action);
		$this->session->set_userdata('menu',$menu);
		
		if(isset($menu)&&is_array($menu['base_mnu'])){
			$tmp_a1 = array_values($menu['base_mnu']);
			$tmp_a2 = array_values($tmp_a1[0]);
			$tmp_a3 = array_values($tmp_a2[0]);
			$ctrlName = $tmp_a3[0];
		}


		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_login'));
		$this->response->setAddon(array('url' => base_url('admin/'.$ctrlName)));
		$this->response->responseExit();
	}

	public function logout(){
		$this->session->sess_destroy();
		redirect('admin/login');
	}

	public function captcha(){
		include APPPATH.'third_party/simple-php-captcha/simple-php-captcha.php';
		
        simple_php_captcha($code,array(
                'width' => 55,
                'height' => 25,
                'angle_max' => 2,
                'min_font_size' => 12,
                'max_font_size' => 12
        ));
        $this->session->set_userdata('captcha',md5(strtolower($code).SALT));
	}

}
/* End of file login.php */
/* Location: ./application/controllers/login.php */